package com.rentong.wx.cust.controller;

import com.rentong.common.R;
import com.rentong.wx.cust.domain.CustServiceOrderEntity;
import com.rentong.wx.cust.domain.CustServiceOrderPingjiaEntity;
import com.rentong.wx.cust.domain.CustServiceOrderShouhouEntity;
import com.rentong.wx.cust.dto.*;
import com.rentong.wx.cust.service.CustServiceOrderPingjiaService;
import com.rentong.wx.cust.service.CustServiceOrderService;
import com.rentong.wx.cust.service.CustServiceOrderShouhouService;
import com.rentong.wx.utils.TencentMapReverseGeocoding;
import com.rentong.wx.utils.WxApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author zhangle
 * @email 459343178@qq.com
 * @date 2024-12-24 14:42:32
 */
@RestController
public class CustServiceOrderController {

    @Autowired
    private CustServiceOrderService custServiceOrderService;

    @Autowired
    private CustServiceOrderPingjiaService cargoServiceOrderPingjiaService;

    @Autowired
    private CustServiceOrderShouhouService custServiceOrderShouhouService;

    /**
     * 24、插入服务信息
     **/
    @PostMapping("/custServiceOrder/insertServiceInformation")
    public R insertServiceInformation(HttpServletRequest request, String tuArray[], Integer[] tuType, ServiceAddressDto serviceAddressDto) throws Exception {
        Integer integer = custServiceOrderService.insertServiceInformation(WxApiUtil.getCurrentCustId(request), tuArray, tuType, serviceAddressDto);
        if (integer > 0) {
            return R.success("操作成功");
        } else {
            return R.error("操作失败");
        }
    }


    //105、派单大厅
    @PostMapping("/custServiceOrder/getCustServiceOrderList")
    public R getCustServiceOrderList(CustServiceOrderDto custServiceOrderDto, HttpServletRequest request) throws Exception {
        custServiceOrderDto.setCustId(WxApiUtil.getCurrentCustId(request));
        return R.success(custServiceOrderService.getCustServiceOrderList(custServiceOrderDto));
    }

    //106、派单详情
    @PostMapping("/custServiceOrder/getCusServiceOrderInfo")
    public R getCusServiceOrderInfo(Long serviceOrderId) {
        return R.success(custServiceOrderService.getCusServiceOrderInfo(serviceOrderId));
    }

    //107、师傅订单列表（也包含用户端订单列表）
    @PostMapping("/custServiceOrder/getChefOrderList")
    public R getChefOrderList(ChefOrderListDto chefOrderListDto, HttpServletRequest request) throws Exception {
        return R.success(custServiceOrderService.getChefOrderList(WxApiUtil.getCurrentCustId(request), chefOrderListDto));
    }

    //112、师傅端去报价或去抢单
    @PostMapping("/custServiceOrder/baojia")
    public R baojia(baojiaDto baojiaDto, HttpServletRequest request) throws Exception {
        return R.success(custServiceOrderService.baojia(baojiaDto, WxApiUtil.getCurrentCustId(request)));
    }

    //305、师傅取消待预约
    @PostMapping("/custServiceOrder/cancelYuYue")
    public R cancelYuYue(Long serviceOrderId, HttpServletRequest request) throws Exception {
//        custServiceOrderService.cancelServiceOrder(serviceOrderId,WxApiUtil.getCurrentCustId(request));
        custServiceOrderService.cancelYuYue(serviceOrderId, WxApiUtil.getCurrentCustId(request));
        return R.success("操作成功");
    }

    //113、师傅端确认接单或拒绝接单
    @PostMapping("/custServiceOrder/confirmOrCancelOrder")
    public R confirmOrCancelOrder(Long serviceOrderId, Integer type, HttpServletRequest request) throws Exception {
        custServiceOrderService.confirmOrder(WxApiUtil.getCurrentCustId(request), serviceOrderId, type);
        return R.success("操作成功");
    }

    //114、用户端订单列表去选择师傅,展示可选择的师傅列表
    @PostMapping("/custServiceOrder/getOrderChefList")
    public R getOrderChefList(Long serviceOrderId, BigDecimal longitude, BigDecimal latitude) {
        return R.success(custServiceOrderService.getOrderChefList(serviceOrderId, longitude, latitude));
    }

    //115、师傅端开始服务或结束服务
    @PostMapping("/custServiceOrder/startService")
    public R startService(StartServiceDto startServiceDto, HttpServletRequest request) throws Exception {
        Long custId = WxApiUtil.getCurrentCustId(request);
        custServiceOrderService.startService(startServiceDto, custId);
        return R.success();
    }

    //116、用户端提交订单评价
    @PostMapping("/custServiceOrder/submitPingjia")
    public R submitPingjia(CustServiceOrderPingjiaEntity custServiceOrderPingjia, Long serviceOrderId, HttpServletRequest request) throws Exception {
        cargoServiceOrderPingjiaService.submitPingjia(custServiceOrderPingjia, serviceOrderId, WxApiUtil.getCurrentCustId(request));
        return R.success("操作成功！");
    }

    //117、用户端申请售后
    @PostMapping("/custServiceOrder/submitShouhou")
    public R submitShouhou(CustServiceOrderShouhouEntity shouhouEntity, Integer type) throws Exception {
        custServiceOrderShouhouService.submitShouhou(shouhouEntity, type);
        return R.success("操作成功！");
    }

    //118、用户端查看售后详情
    @PostMapping("/custServiceOrder/getShouhou")
    public R getShouhou(Long serviceOrderId) {
        return R.success(custServiceOrderShouhouService.getShouhou(serviceOrderId));
    }

    //119、用户端订单列表
    @PostMapping("/custServiceOrder/getUserOrderList")
    public R getUserOrderList(CustServiceOrderEntityDto custServiceOrderEntityDto, HttpServletRequest request) throws Exception {
        custServiceOrderEntityDto.setCustId(WxApiUtil.getCurrentCustId(request));
        return R.success(custServiceOrderService.getUserOrderList(custServiceOrderEntityDto));
    }


    //218、确认支付、找人代付按钮（代付人打开页面，拉起支付，不是这个接口，是接口219）
    @PostMapping("/custServiceOrder/insertOrder")
    public R insertOrder(CustServiceOrderEntityDto custServiceOrderEntityDto, HttpServletRequest request) throws Exception {
        custServiceOrderEntityDto.setCustId(WxApiUtil.getCurrentCustId(request));
        return R.success(custServiceOrderService.insertOrder(custServiceOrderEntityDto));
    }

    //219、找人代付（代付人打开页面支付）
    @PostMapping("/custServiceOrder/daifuOrder")
    public R daifuOrder(CustServiceOrderEntityDto custServiceOrderEntityDto, HttpServletRequest request) throws Exception {
        custServiceOrderEntityDto.setCustId(WxApiUtil.getCurrentCustId(request));
        return R.success(custServiceOrderService.daifuOrder(custServiceOrderEntityDto));
    }

    //220、取消支付（拉起微信输入密码框之后，点击右上角小叉叉的取消支付）
    @PostMapping("/custServiceOrder/cancelOrder")
    public R cancelOrder(CustServiceOrderEntity custServiceOrderEntity) {
        custServiceOrderService.cancelOrder(custServiceOrderEntity);
        return R.success("操作成功！");
    }

    //229、订单详情
    @RequestMapping("/custServiceOrder/getOrderByServiceOrderId")
    public R getOrderByServiceOrderId(CustServiceOrderEntityDto custServiceOrderEntityDto, HttpServletRequest request) throws Exception {
        custServiceOrderEntityDto.setCustId(WxApiUtil.getCurrentCustId(request));
        return R.success(custServiceOrderService.getOrderByServiceOrderId(custServiceOrderEntityDto));
    }

    //120、用户端取消订单
    @PostMapping("/custServiceOrder/cancelServiceOrder")
    public R cancelOrder(UpdateOrderDto updateOrderDto) throws Exception {
        return R.success(custServiceOrderService.cancelOrder(updateOrderDto, null));
    }

    //121、用户端拒绝师傅
    @PostMapping("/custServiceOrder/refuseChef")
    public R refuseChef(UpdateOrderDto updateOrderDto) {
        custServiceOrderService.refuseChef(updateOrderDto);
        return R.success();
    }

    //122、用户端选择师傅(商家报价和师傅报价用这个,到家私厨·项目预约用128)
    @PostMapping("/custServiceOrder/chooseChef")
    public R chooseChef(UpdateOrderDto updateOrderDto) {
        return R.success(custServiceOrderService.chooseChef(updateOrderDto));
    }

    //124、删除订单
    @PostMapping("/custServiceOrder/deleteOrder")
    public R deleteOrder(CustServiceOrderEntity custServiceOrderEntity) {
        return R.success(custServiceOrderService.deleteOrder(custServiceOrderEntity.getServiceOrderId()));
    }

    //125、根据经纬度获取详细地址
    @PostMapping("/custServiceOrder/getAddressDetail")
    public R getAddressDetail(Double longitude, Double latitude) {
        try {
            return R.success(TencentMapReverseGeocoding.getAMapByLngAndLat(longitude.toString(), latitude.toString()));
        } catch (Exception e) {
            return null;
        }
    }

    //126、确认订单
    @PostMapping("/custServiceOrder/confirmServiceOrder")
    public R confirmServiceOrder(Long serviceOrderId) {
        custServiceOrderService.confirmServiceOrder(serviceOrderId);
        return R.success();
    }

    //128 项目预约的订单去选择师傅
    @PostMapping("/custServiceOrder/pickChef")
    public R pickChef(Long serviceOrderId, Long chefId) {
        custServiceOrderService.pickChef(serviceOrderId, chefId);
        return R.success();
    }

    //131、师傅端售后，拒绝或同意售后
    @PostMapping("/custServiceOrder/handleShouhou")
    public R handleShouhou(Long serviceOrderId, Long type, String refuse) {
        custServiceOrderService.handleShouhou(serviceOrderId, type,refuse);
        return R.success();
    }

}